<?php

namespace app\app\controller;

use app\app\model\ProjectModel;
use app\app\model\ProjectProcessModel;
use app\app\model\ProjectTowerImgModel;
use app\app\model\ProjectTowerModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;


class ProjectTower extends BaseCurd
{
    /**
     * 根据项目id获取所有塔号和状态
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function towerList()
    {
        $project_id = input('project_id', '');

        $list['data'] = ProjectTowerModel::field('id,t_no,state,latitude,longitude')
            ->where('project_id', $project_id)->where('state', '>', '0')->select();
        $list['describe'] = ProjectModel::field('id,describe')->where('id', $project_id)->find();

        $this->success('success', $list);
    }

    public function towerLists()
    {
        $project_id = input('project_id', '');
        $list = ProjectTowerModel::field('id,t_no,state,latitude,longitude')
            ->where('t_no', '<>', '')
            ->where('project_id', $project_id)
            ->where('state', '>', '0')
            ->select();
        $this->success('success', $list);
    }


    /**
     * 进度上报
     */
    public function update()
    {
        $data = $this->request->param();
        $project_id = $data['project_id'];
        $info = json_decode($data['info'], true);

        foreach ($info as $key => $value) {
            $update['state'] = $value['state'];
            ProjectTowerModel::where('id', $value['id'])->update($update);
        }
        if (isset($data['file_id']) && !empty($data['file_id'])) {
            $add['project_id'] = $data['project_id'];
            $add['file_id'] = $data['file_id'];
            if (isset($data['remark'])) $add['remark'] = $data['remark'];
            ProjectTowerImgModel::create($add);
            // var_dump(ProjectTowerImgModel::getLastSql());die();
        }

        if (isset($data['finish_total']) || isset($data['describe'])) {
            if (isset($data['finish_total']) && !empty($data['finish_total'])) $project['finish_total'] = $data['finish_total'];
            if (isset($data['describe']) && !empty($data['finish_total'])) $project['describe'] = $data['describe'];
            ProjectModel::where('id', $project_id)->update($project);
        }

        $this->success('success');
    }

    public function peojectCensus()
    {
        $project_id = input('project_id');
        $project['census'] = input('census');
        ProjectModel::where('id', $project_id)->update($project);
    }


    /**
     * 线路获取上传图片信息
     */
    public function towerImg()
    {
        $project_id = input('project_id');
        $list = ProjectTowerImgModel::alias('a')->field('a.*,b.file_path,b.file_name')
            ->leftJoin('sys_file_info b', 'a.file_id = b.file_id')
            ->order('a.create_time desc')
            ->where('a.project_id', $project_id)->select();
        $this->success('success', $list);
    }

    /**
     * 进度统计
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function analysis()
    {
        $project_id = input('project_id', '');

        $total = 0;         //总数
        $base_build = 0;    //基础在建
        $base_finish = 0;   //基础完成
        $tower_build = 0;   //立塔在建
        $tower_finish = 0;  //立塔完成
        $line_total = 0;    //线路总长度
        $line_finish = 0;   //线路完成长度

        $project_info = ProjectModel::field('id,project_scale_2,finish_total')->where('id', $project_id)->find();

        if ($project_info) {
            if ($project_info['project_scale_2']) {
                $line_total = $project_info['project_scale_2'];
            }
            if ($project_info['finish_total']) {
                $line_finish = $project_info['finish_total'];
            }
        }

        $list = ProjectTowerModel::field('state,count(id) as total')->where('project_id', $project_id)
            ->group('state')->order('state asc')->select()->toArray();

        if ($list) {
            foreach ($list as $key => $value) {
                $total += $value['total'];
                switch ($value['state']) {
                    case 1:
                        break;
                    case 2:
                        $base_build += $value['total'];
                        break;
                    case 3:
                        $base_build += $value['total'];
                        $base_finish += $value['total'];
                        break;
                    case 4:
                        $base_build += $value['total'];
                        $base_finish += $value['total'];
                        $tower_build += $value['total'];
                        break;
                    case 5:
                    case 6:
                    case 7:
                        $base_build += $value['total'];
                        $base_finish += $value['total'];
                        $tower_build += $value['total'];
                        $tower_finish += $value['total'];
                        break;
                }
            }
        }

        $info = array(
            'total' => $total,
            'base_build' => $base_build,
            'base_finish' => $base_finish,
            'tower_build' => $tower_build,
            'tower_finish' => $tower_finish,
            'line_total' => $line_total,
            'line_finish' => $line_finish
        );
        $this->success('success', $info);
    }


    /**
     * 变电站进度上报
     */
    public function processAdd()
    {
        $data = $this->request->param();
        $list = ProjectProcessModel::where('project_id', $data['project_id'])->where('type', $data['type'])->find();
        if ($list) {
            ProjectProcessModel::strict(false)->where('id', $list['id'])->update($data);
        } else {
            ProjectProcessModel::create($data);
        }

        if (isset($data['file_id']) && !empty($data['file_id'])) {
            $add['project_id'] = $data['project_id'];
            $add['file_id'] = $data['file_id'];
            if (isset($data['remark'])) $add['remark'] = $data['remark'];
            ProjectTowerImgModel::create($add);
        }

        if (isset($data['describe'])) {
            $project['describe'] = $data['describe'];
            ProjectModel::where('id', $data['project_id'])->update($project);
        }

        $this->success('success');
    }

    /**
     * 变电站进度详情
     */
    public function processList()
    {
        $project_id = input('project_id');
        $type = input('type');

        $list['data'] = ProjectProcessModel::where('project_id', $project_id)->where('type', $type)->find();
        $list['describe'] = ProjectModel::field('id,describe')->where('id', $project_id)->find();
        $this->success('success', $list);
    }

    /**
     * initialize
     * @return void
     */
    public function initialize()
    {
        $this->model = ProjectTowerModel::class;
    }

    /**
     * 查询项目列表
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function listGroup()
    {
        $name = input('project_name/s', '');

        $start_time = date('Y-01-01 00:00:00', time());
        $end_time = date('Y-12-31 23:59:59', time());
        $where[] = ['b.tc_time', '>=', $start_time];
        $where[] = ['b.kg_time', '<=', $end_time];
        if ($name) {
            $where[] = ['b.project_name|c.project_name', 'like', '%' . $name . '%'];
        }
        $where[] = ['a.t_no', '<>', ''];

        $link_user = input('link_user', '');
        $type = input('type', '');
        if ($type == '项目经理') {
            $where[] = ['b.link_user_id|c.link_user_id', 'like', '%' . $link_user . '%'];
        }

        $data = $this->model::alias('a')
            ->leftJoin('t_project b', 'a.project_id = b.id')
            ->leftJoin('t_project c', 'b.link_project_id = c.id')
            ->where($where)
            ->group('b.id')
            ->field('b.id,b.link_project_id,b.is_parent')
            ->select();

        $project_id_array = array_column($data->toArray(), null, 'id');
        $project_id_array = array_keys($project_id_array);

        $result = [];
        foreach ($data as $v) {
            if ($v['is_parent'] == 0) {
                $result[$v['id']] = $v;
                continue;
            }

            $result[$v['link_project_id']]['children'][$v['id']]['children'][] = $v;
        }

        $project_id_array = array_merge($project_id_array, array_keys($result));

        $project = ProjectModel::where('id', 'in', $project_id_array)
            ->field('id, project_name')
            ->select();
        $project = array_column($project->toArray(), null, 'id');

        $result = $this->getProject($result, $project);

        $this->success('success', $result);
    }

    /**
     * 查询指定项目id下的列表
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function ChildrenList()
    {
        $id = input('id/s');
        if (!$id) $this->error('请传入id');
        $page_size = input('pageSize', '20');
        $data = $this->model::where('project_id', $id)
            ->paginate($page_size);
        if (empty($data)) $this->error('未找到符合条件的资源');

        $this->success('success', $data);
    }
}
